{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "32a1df8b-2c47-40a6-8c19-a90191754583",
   "metadata": {},
   "source": [
    "### 1.定义列表读取数据文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "df02dfa5-a805-403e-a09c-858cac745fb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "academy_titles = [] \n",
    "job_titles = []\n",
    "with open(\"academy_titles.txt\", encoding=\"utf-8\", mode=\"r\") as f:\n",
    "    for line in f:\n",
    "        academy_titles.append(line.strip())\n",
    "\n",
    "with open(\"job_titles.txt\", encoding=\"utf-8\", mode=\"r\") as f:\n",
    "    for line in f:\n",
    "        job_titles.append(line.strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e28dc6c4-cb50-4dec-8c2d-40f167534a57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['北师教育学，你我一起努力，让胜利酣畅淋漓。',\n",
       " '考博英语词汇',\n",
       " '出售人大新闻学院2015年考研权威资料',\n",
       " '【脑科院 郭桃梅课题组】科研助理招聘',\n",
       " '管理学院的同学帮帮忙呐～']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "academy_titles[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "980c93f1-1d7c-4690-bf75-3d2cc8d4e395",
   "metadata": {},
   "source": [
    "### 2.统计数据集中出现的字符数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "12994448-866d-4582-8ac4-5e22f3954e5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1570\n"
     ]
    }
   ],
   "source": [
    "char_set = set() #定义set集合用于字符去重\n",
    "for title in academy_titles:\n",
    "    for ch in title:\n",
    "        char_set.add(ch)\n",
    "\n",
    "for title in job_titles:\n",
    "    for ch in title:\n",
    "        char_set.add(ch)\n",
    "\n",
    "print(len(char_set))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1b521d9-75aa-4820-b884-553da3336f6f",
   "metadata": {},
   "source": [
    "### 3.词嵌入表示标题数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0481aeb8-9730-4ce0-9071-b35800b8262c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "char_list = list(char_set)\n",
    "n_chars = len(char_list) + 1 #<UNK>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "62aa6239-9e08-4cca-884d-32a51585b936",
   "metadata": {},
   "outputs": [],
   "source": [
    "def title_to_tensor(title):\n",
    "    tensor = torch.zeros(len(title), dtype=torch.long)\n",
    "    for li, ch in enumerate(title):\n",
    "        try:\n",
    "            ind = char_list.index(ch)\n",
    "        except ValueError:\n",
    "            ind = n_chars - 1\n",
    "        tensor[li] = ind\n",
    "    return tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f9812366-3d54-499b-8996-dbe981d73f95",
   "metadata": {},
   "outputs": [],
   "source": [
    "embedding = torch.nn.Embedding(n_chars, 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8203737f-f637-497f-b954-5e54be580d0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "招聘兼职/ 笔试考务 /200-300 每人\n"
     ]
    }
   ],
   "source": [
    "print(job_titles[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "255bea75-c8cb-45af-8a9b-f8def667e9b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([ 819,  406,  474,  130, 1489, 1460,  107, 1042,  328,  520, 1460, 1489,\n",
      "         361, 1206, 1206,  973,  146, 1206, 1206, 1460, 1153,  423])\n"
     ]
    }
   ],
   "source": [
    "print(title_to_tensor(job_titles[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "984e58c0-e8d3-4d44-892d-1d40256f7ab8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
